package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseTimeDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.utils.DateUtil;
import java.util.Date;

public class ManageReimburseListDetailFragment extends Fragment implements OnClickListener {
  protected BaseVolleyActivity mActivity;
  protected CurrentUser mCurrentUser;
  private static final int APPEND_DATE = 0;//发生日期
  private static final int Fare_Type = 1;//费用分类
  protected TextView tvappendDate;
  protected TextView tvfareType;
  protected EditText etfeeAmount;
  protected EditText etbillNum;
  protected EditText etbillNo;
  protected EditText etfeeNote;
  protected EditText etnote;
  protected ImageView ibappendDate;
  protected ImageView ibfareType;
  protected TextView tvInvestigateContent;//审查修改附言
  protected String mfeeKindName;//费用分类名
  protected String mfeeKindId;//费用分类id
  protected String mfeeKindLen = "";//费用分类len
  protected String mfeeKindFlag = "";//费用分类flag

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    ((BaseVolleyActivity) getActivity()).getSupportActionBar()
        .setTitle(R.string.manageReimburseList);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    mActivity = (BaseVolleyActivity) getActivity();
    mCurrentUser = CurrentUser.newInstance(getActivity());
    //		setTitle();
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_add_manage_reimburse_list, container, false);
    tvappendDate = (TextView) v.findViewById(R.id.tv_appendDate);
    tvfareType = (TextView) v.findViewById(R.id.tv_fareType);
    etfeeAmount = (EditText) v.findViewById(R.id.et_fee_amount);
    etbillNum = (EditText) v.findViewById(R.id.et_bill_num);
    etbillNo = (EditText) v.findViewById(R.id.et_billNo);
    etfeeNote = (EditText) v.findViewById(R.id.et_fee_note);
    etnote = (EditText) v.findViewById(R.id.et_note);
    tvInvestigateContent = (TextView) v.findViewById(R.id.tv_investigate_content);

    ibappendDate = (ImageView) v.findViewById(R.id.ib_appendDate);
    ibfareType = (ImageView) v.findViewById(R.id.ib_fareType);
    ibappendDate.setOnClickListener(this);
    ibfareType.setOnClickListener(this);
    initData();
    return v;
  }

  protected void initData() {
  }

  @Override public void onClick(View arg0) {
    switch (arg0.getId()) {
      case R.id.ib_appendDate:
        showDialog(APPEND_DATE, new Date());
        break;
      case R.id.ib_fareType:
        showDialog(Fare_Type, new Date());
        break;

      default:
        break;
    }
  }

  public void showDialog(int requestCode, Date date) {
    DialogFragment dialog = null;
    FragmentManager fm = getActivity().getSupportFragmentManager();
    switch (requestCode) {
      case APPEND_DATE:
        dialog = dialog = new BaseYMDTimeDialogFragment();
        break;

      case Fare_Type:
        dialog = new FeeKindDialogFragment();

        break;

      default:
        break;
    }
    dialog.setTargetFragment(ManageReimburseListDetailFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == APPEND_DATE && data != null) {
      Date chooseDate = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
      String chooseDateString = DateUtil.dateToString(chooseDate, DateUtil.TYPE_DATE);
      tvappendDate.setText(chooseDateString);
      return;
    }
    if (requestCode == Fare_Type && data != null) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      mfeeKindName = customer.getName();
      mfeeKindId = customer.getId();
      mfeeKindLen = customer.getJobNo();
      mfeeKindFlag = customer.getMemberCode();
      if (mfeeKindLen == null) {
        mfeeKindLen = "";
      }
      if (mfeeKindFlag == null) {
        mfeeKindFlag = "";
      }
      tvfareType.setText(mfeeKindName);
      return;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  //判断是否所有都为空
  public boolean allEmpty() {
    if (TextUtils.isEmpty(tvappendDate.getText().toString())
        && TextUtils.isEmpty(tvfareType.getText().toString())
        && TextUtils.isEmpty(etfeeAmount.getText().toString())
        && TextUtils.isEmpty(etbillNum.getText().toString())
        && TextUtils.isEmpty(etbillNo.getText().toString())
        && TextUtils.isEmpty(etfeeNote.getText().toString())
        && TextUtils.isEmpty(etnote.getText().toString())) {
      return true;
    } else {
      return false;
    }
  }
}
